#include "LCD_Test.h"
#define PINK_EMOJI 0
#define RED_EMOJI 1
#define YELLOW_EMOJI 2
#define BLUE_EMOJI 3
#define GREEN_EMOJI 4
#define MODE_USUAL 0
#define MODE_LIGHT 1
#define MODE_DARK 2
//绘制图像
void DrawImage(const UWORD *img, UWORD width,UWORD height, UWORD start_x,UWORD start_y,UWORD bg_color){
  Paint_Clear(bg_color);
  for(UWORD i=0;i<height*2;i++){
    for(UWORD j=0;j<width*2;j++){
      UWORD color;
      if(j<5 || i <5){
        color=bg_color;
      }else{
        color=img[(i/2)*width+(j/2)];
      }
      // Paint_DrawPoint(j+start_x,i+start_y,color,DOT_PIXEL_1X1, DOT_FILL_RIGHTUP);
       Paint_SetPixel(j+start_x,i+start_y,color);
    }
  }
  
}

uint32_t acc_clock=0;
uint32_t acc_rock=0;
uint8_t  mode=0;
void setup() {
  // put your setup code here, to run once:
  if(DEV_Module_Init() != 0)
    Serial.println("GPIO Init Fail!");
  else
    Serial.println("GPIO Init successful!");

  LCD_1IN28_Init(HORIZONTAL);
  DEV_SET_PWM(50);
  LCD_1IN28_Clear(WHITE);
  UDOUBLE Imagesize = LCD_1IN28_HEIGHT * LCD_1IN28_WIDTH * 2;
  UWORD *BlackImage;
  if ((BlackImage = (UWORD *)malloc(Imagesize)) == NULL)
  {
      Serial.println("Failed to apply for black memory...");
      exit(0);
  }
  // /*1.Create a new image cache named IMAGE_RGB and fill it with white*/
  Paint_NewImage((UBYTE *)BlackImage, LCD_1IN28.WIDTH, LCD_1IN28.HEIGHT, 0, WHITE);
  Paint_SetScale(65);
  Paint_Clear(WHITE);
  Paint_SetRotate(ROTATE_180);
  Paint_Clear(WHITE);
#if 1
  Paint_DrawString_EN(50, 100, "Mamawhes", &Font16, RED, WHITE);

  // /*3.Refresh the picture in RAM to LCD*/
  LCD_1IN28_Display(BlackImage);
  DEV_Delay_ms(2000);
  Paint_Clear(BLACK);
#endif
  // /* GUI */
  Serial.println("drawing...\r\n");
  // /*2.Drawing on the image*/

    float acc[3], gyro[3];
    unsigned int tim_count = 0;
    float result;
    uint32_t gacc=0;
    uint8_t emoji=0;
    QMI8658_init();
    Serial.println("QMI8658_init\r\n");
    const float conversion_factor = 3.3f / (1 << 12) * 2;
    uint32_t img_ptr=0;
    // 0xf2f6 28 PINK 
    // 0xe061 32 RED
    // 0xf72a 25 YELLOW
    // 0xb5b 20 BLUE
    // 0x176f 34 GREEN
    while (true)
    {
        QMI8658_read_xyz(acc, gyro, &tim_count);
        gacc=abs(gyro[0])+ abs(gyro[1])+abs(gyro[2]);

        result = DEC_ADC_Read()* conversion_factor;
        if(result<3.73){
          Paint_Clear(BLACK);
          Paint_DrawString_EN(50, 100, "Low Power!", &Font16, RED, BLACK);
         
          LCD_1IN28_Display(BlackImage);
          continue;
        }else if(result<3.69){
          DEV_SET_PWM(0);
          DEV_Delay_ms(3600000);
        }
        if(gacc>1200){
          emoji=++emoji%5;
          acc_rock++;
          img_ptr=0;
          acc_clock=0;
        }else if(gacc<100){
          acc_clock++;
        }else{
          acc_clock=0;
        }
        if(mode==MODE_USUAL){
          if(acc_clock>200){
            DEV_SET_PWM(0);
            continue;
          }
          if(acc_clock>50){
            DEV_SET_PWM(10);
          }
          if(acc_clock==0){
            DEV_SET_PWM(50);
          }
        }else if(mode==MODE_DARK){
          DEV_SET_PWM(10);
        }else if(mode==MODE_LIGHT){
          DEV_SET_PWM(50);
        }

         if(acc_clock>5){
          if(acc_rock>10){
            if(acc_rock>20){
              mode=MODE_DARK;
            }else if(acc_rock>15){
               mode=MODE_LIGHT;
            }else if(acc_rock>10){
              mode=MODE_USUAL;
            }
          }
          acc_rock=0;
        }

         if(acc_rock>25){
          acc_rock=11;
         }
        if(acc_rock>20){
          Paint_Clear(BLUE);
          LCD_1IN28_Display(BlackImage);
          continue;
        }else if(acc_rock>15){
          Paint_Clear(GREEN);
          LCD_1IN28_Display(BlackImage);
           continue;
        }else if(acc_rock>10){
          Paint_Clear(RED);
          LCD_1IN28_Display(BlackImage);
          
           continue;
        }
        if(emoji==PINK_EMOJI){
          DrawImage(Pink_IMG+img_ptr,90,50,30,60,0xf2f6);
          // LCD_1IN28_Display(BlackImage);
          img_ptr+=90*50;
          if(img_ptr>=90*50*28){
            img_ptr=0;
          }
        }
        else if(emoji==RED_EMOJI){
          DrawImage(Red_IMG+img_ptr,90,50,30,60,0xe061);
          // LCD_1IN28_Display(BlackImage);
          img_ptr+=90*50;
          if(img_ptr>=90*50*32){
            img_ptr=0;
          }
        }
        else if(emoji==YELLOW_EMOJI){
          DrawImage(Yellow_IMG+img_ptr,90,50,30,60,0xf72a);
          // LCD_1IN28_Display(BlackImage);
          img_ptr+=90*50;
          if(img_ptr>=90*50*25){
            img_ptr=0;
          }
        }
        else if(emoji==BLUE_EMOJI){
          DrawImage(Blue_IMG+img_ptr,90,50,30,60,0xb5b);
          // LCD_1IN28_Display(BlackImage);
          img_ptr+=90*50;
          if(img_ptr>=90*50*20){
            img_ptr=0;
          }
        }
        else if(emoji==GREEN_EMOJI){
          DrawImage(Green_IMG+img_ptr,90,50,30,60,0x176f);
          // LCD_1IN28_Display(BlackImage);
          img_ptr+=90*50;
          if(img_ptr>=90*50*34){
            img_ptr=0;
          }
        }
        LCD_1IN28_Display(BlackImage);
//        DEV_Delay_ms(100);
    }
}

void loop() {
}
